'use strict';

stareal
    .controller("ReBindMyCodeController", function ($scope, $api, $stateParams, $alert, $document, localStorageService, $state, $interval,$http,base64) {
        $scope.telphone_no = $stateParams.mobile;
        $scope.accessToken = "";
        $scope.password = "";
        $scope.password2 = "";
        $scope.paracont = "获取验证码";
        $scope.code = "";
        $scope.mapcode='';
        $scope.codeStatus=false;
        $("#login_registercontainer").height($(window).height());
        $("#login_registercontainer").height(window.screen.height);
        $scope.H5CorderId=localStorageService.get('H5CorderId');
        $scope.getmyCode=function(){//app/gif/getGifCode
            var strdate=base64.encode(new Date().getTime().toString());
            var str1='',str2='';
            var arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c',
                'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
                'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
                'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
                'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
            for(var i=0; i<4; i++){
                var pos = Math.round(Math.random() * (arr.length-1));
                str1 += arr[pos];
            }
            for(var i=0; i<5; i++){
                var pos = Math.round(Math.random() * (arr.length-1));
                str2 += arr[pos];
            }
            var Str=str1+strdate+ str2;
            var encrypt=base64.encode(Str.toString());
            //var url = 'https://api.mydeershow.com/mobile/app/view/create?source=pc&accessToken='+token;//正式
            // var url = 'http://apis.blackwan.cn/mobile/app/view/create?source=pc&accessToken='+token;//测试
           //$scope.verificationGift = 'http://111.231.0.33:9084/mobile/app/gif/getGifCode?source=mobile&encrypt='+encrypt+'&mobile='+$stateParams.mobile;//测试
            $scope.verificationGift = 'https://test.djx.api02.opentickets.cn/mobile/app/gif/getGifCode?source=mobile&encrypt='+encrypt+'&mobile='+$stateParams.mobile;//测试
          //$scope.verificationGift = 'https://api.mydeershow.com/mobile/app/gif/getGifCode?source=mobile&encrypt='+encrypt+'&mobile='+$stateParams.mobile;//正式
            //sconsole.log($scope.verificationGift);
            var ImgStr=' <img src="'+$scope.verificationGift+'" alt="" style="width: 2.5rem;height: 0.8rem;dsiplay:block;">';
            $("#verification_Gift").html(ImgStr);
            //console.log('22');
        }
        $scope.getmyCode();
        $scope.login = {
            sendCode:function (){
                var second = 60;
                var timerHandler = undefined;
                if($scope.codeStatus){
                    return;
                }
                if (!this.validatemobile($scope.telphone_no)){
                    return;
                }
                if($("#mapcode").val()==''||$("#mapcode").val()==undefined||$("#mapcode").val()==null){
                    $alert.show("请输入图形验证码!");
                    return;
                }else{
                    $scope.mapcode=$("#mapcode").val().trim();
                }
                //$scope.mapcode   app/slidingVerificationCode/verifySelf
                // // 验证码
                $api.post("app/slidingVerificationCode/verifySelf", {mobile:$scope.telphone_no,type: "0",verification:$scope.mapcode})
                    .then(function (ret) {
                        if (ret.retCode == "0") {
                            $alert.show("验证码已发送!");
                            localStorageService.set('code_token', ret.accessToken);
                            timerHandler = $interval(function () {
                                if (second <= 0) {
                                    $interval.cancel(timerHandler);
                                    timerHandler = undefined;
                                    second = 60;
                                    $scope.paracont = "重发";
                                    $scope.codeStatus=false;
                                } else {
                                    $scope.codeStatus=true;
                                    $scope.paracont = second + "秒";
                                    second--;

                                }
                            },1000)

                        } else {
                            $alert.show("验证码发送失败，请稍后重试!");
                        }
                    },function(err){
                        $alert.show(err);
                        // timerHandler =undefined
                    });
                // if (timerHandler) {
                //     return;
                // }

            },
            login:function (){
                // console.log(localStorageService.get('code_token'))
                if (!this.validatemobile($scope.telphone_no)) {
                    return;
                }
                if (!localStorageService.get('code_token')) {
                    $alert.show("请先获取验证码！");
                    return false;
                }
                if (!$scope.code||$("#num_code").val()==""||$("#num_code").val()==undefined||$("#num_code").val()==null) {
                    $alert.show("验证码不能为空");
                    return false;
                }
                var _params = {
                    mobile:$scope.telphone_no,
                    code:$("#num_code").val().trim(),
                    // password:$scope.password,
                    smsToken:localStorageService.get('code_token'),
                    accessToken: localStorageService.get('cleartoken'),
                    openid:localStorageService.get('unionid'),
                    plat: 'wx'
                };
                $api.post("app/login/user/reBind", _params)
                    .then(function (ret) {
                        $alert.show("绑定成功");
                         localStorageService.set('isbind',1);
                        localStorageService.set('token',localStorageService.get('cleartoken'));
                        if($scope.H5CorderId!=undefined&& $scope.H5CorderId!=null&& $scope.H5CorderId!=''){
                            var ua = navigator.userAgent.toLowerCase();
                            $scope.IP=returnCitySN["cip"];  //app/channel/register
                            $api.post("app/channel/register", {
                                device_msg:ua,
                                ip:$scope.IP,
                                code:$scope.H5CorderId,
                                source:'mobile'
                            },true).then(function (ret){
                                console.log(ret);
                                localStorageService.set('H5CorderId','');
                            },function(err){
                                $alert.show(err);
                            });
                        }
                        if(_czc!=undefined&&_czc!=null&&_czc!='') {
                            _czc.push(["_trackEvent", 'h5-授权-绑定成功', '绑定成功', '查看', 'rebind']);
                        }
                        var ua = window.navigator.userAgent.toLowerCase();
                        if (ua.match(/MicroMessenger/i) == 'micromessenger'){
                            // 正式地址
                            location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?" +
                                "appid=wxda73ac8ac7af1261&" +
                                "redirect_uri=http%3A%2F%2Ftest.djx.m.opentickets.cn%2Foauth%2Findex" +
                                "&response_type=code&scope=snsapi_userinfo&state="+encodeURIComponent($stateParams.good_id)+'&connect_redirect=1#wechat_redirect';
                        }else{
                            $state.go('main.index',{},true);
                        }
                    }, function (err) {
                        $alert.show(err);
                    });
            },
            validatemobile:function (mobile){
                if(!mobile){
                    $alert.show("请输入手机号")
                    return false
                }
                if (mobile.toString().length == 0) {
                    $alert.show('请输入手机号码！');
                    return false;
                }
                if (mobile.toString().length != 11) {
                    $alert.show('请输入11位手机号码！');
                    return false;
                }
                var myreg = /^1[3|4|5|6|7|8|9][0-9]{9}$/; //验证规则
                if (!myreg.test(mobile)) {
                    $alert.show('请输入有效的手机号码！');
                    return false;
                }
                return true;
            }
        }
        //清除
        $scope.clearNumber=function(){
            $scope.telphone_no = "";
        }
        //查看密码
        $scope.scanNumber=function(){
            if(!$scope.scanStatus){
                $scope.scanStatus=true;
                $("#useSendCodeInput").attr('type','text');
            }else{
                $scope.scanStatus=false;
                $("#useSendCodeInput").attr('type','password');
            }

        }
    });